package com.sangu.dianxiu.utils;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.config.Constants;

import java.util.Map;

import cz.msebera.android.httpclient.Header;

/**用户管理接口的实现类，用来进行网络请求
 * Created by sg2 on 2016/1/4.
 */
public class UserManager{
    //使用volley框架进行数据请求
    //网络请求队列
    public RequestQueue requestQueue= Volley.newRequestQueue(BaseApp.getInstance());
    //网络请求
    private StringRequest request;
    //
    private StringRequest getRequest(final Map<String, String> map,final ICallBack callBack,String url)
    {
        StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                callBack.onSuccess(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                callBack.onError(error);
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError { //请求参数
                return map;
            }
        };
        stringRequest.setTag(UserManager.class);
        stringRequest.setShouldCache(true);
        return stringRequest;
    }
    //获得验证码的数据请求接口

    public void getVerificationCode(Map<String, String> map,ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_GET_VERIFICATIONCODE);
        requestQueue.add(request);
    }
    //登录数据请求接口

    public void login(final Map<String, String> map, final ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_LOGIN);
        requestQueue.add(request);
    }
    //退出登录数据请求接口

    public void logout(Map<String, String> map, final ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_LOGOUT);
        requestQueue.add(request);
    }
    //获得联系人的数据请求接口

    public void getContacts(Map<String, String> map, final ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_GET_CONTACTS);
        requestQueue.add(request);
    }
    //添加联系人的数据请求接口

    public void addContacts(Map<String, String> map, final ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_ADD_CONTACTS);
        requestQueue.add(request);
    }
    //删除联系人的数据请求接口

    public void deleteContacts(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_DELETE_CONTACTS);
        requestQueue.add(request);
    }
    //设置默认联系人的数据请求接口

    public void setDefaultContacts(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_SET_DEFAULT_CONTACTS);
        requestQueue.add(request);
    }
    //获得地址的数据请求接口

    public void getAddress(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_GET_ADDRESS);
        requestQueue.add(request);
    }
    //添加地址的数据请求接口

    public void addAddress(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_ADD_ADDRESS);
        requestQueue.add(request);
    }
    //删除地址的数据请求接口

    public void deleteAddress(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_DELETE_ADDRESS);
        requestQueue.add(request);
    }
    //设置默认地址的数据请求接口

    public void setDefaultAddress(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_SET_DEFAULT_ADDRESS);
        requestQueue.add(request);
    }
    //获得默认地址和联系人的数据请求接口

    public void getDefaultContactsAndAddress(Map<String, String> map, ICallBack callBack) {
        request=getRequest(map,callBack,Constants.URL_GET_DEFAULT_CONTACTS_ADDRESS);
        requestQueue.add(request);
    }
}
